package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketLog;
import com.cskaoyan.market.db.domain.MarketLogExample;
import com.cskaoyan.market.db.mapper.MarketLogMapper;
import com.cskaoyan.market.service.AdminLogService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘国兴
 * @description 日志系统管理
 */
public class AdminLogServiceImpl implements AdminLogService {

    @Override
    public List<MarketLog> list(Map<String, String[]> parameterMap) {

        SqlSession session = MybatisUtil.getSession();
        MarketLogMapper mapper = session.getMapper(MarketLogMapper.class);

        MarketLogExample example = new MarketLogExample();
        MarketLogExample.Criteria criteria = example.createCriteria();

        Map<String, String> map = new HashMap<>();
        parameterMap.forEach((key, value) -> map.put(key, StringUtils.join(value)));

        if (StringUtils.isNotEmpty(map.get("name"))) {
            criteria.andAdminLike("%"+map.get("name")+"%");
        }

        example.setOrderByClause(map.get("sort")+" "+map.get("order"));

        PageHelper.startPage(Integer.parseInt(map.get("page")), Integer.parseInt(map.get("limit")));

        List<MarketLog> logList = mapper.selectByExample(example);
        session.commit();
        session.close();
        return logList;
    }
}
